﻿using System;
using System.Collections.Generic;
using System.Text;
using AMS.DataAccess.Sys.BO;
using AMS.DataAccess.Sql;
using AMS.DataAccess.Common;
using AMS.DataAccess.Sys.Filters;
using AMS.DataAccess.Sys.Interfaces;
using System.Data;

namespace AMS.DataAccess.Sys
{
    public class FunctionManager : IFunctionManager
    {
        public const string FunctionTable = "TS_Function";

        public List<FunctionInfo> GetFunctions(CallContext context, int moduleId)
        {
            SqlBuilder sql = new SqlBuilder();
            sql.Add("SELECT * FROM TS_Function");
            if (moduleId != 0)
            {
                sql.Add("WHERE ModuleID = ", moduleId.ToString());
            }

            var ds = SqlDBHelper.Query(sql.ToString());
            var list = BusinessObjectMapping.DataTableToBOList<FunctionInfo>(ds.Tables[0]);
            return list;
        }

        public FunctionInfo GetFunction(CallContext context, int functionId)
        {
            SqlBuilder sql = new SqlBuilder();
            sql.Add("SELECT * FROM TS_Function");
            sql.Add("WHERE FunctionID = ", functionId.ToString());
            var ds = SqlDBHelper.Query(sql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return BusinessObjectMapping.DataRowToBO<FunctionInfo>(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        public int Add(CallContext context, FunctionInfo function)
        {
            SqlInsert sql = new SqlInsert(FunctionTable);
            sql.AddBusinessObject(function);
            sql.Add("SELECT @@IDENTITY");

#if SHOW_SQL
            System.Diagnostics.Debug.WriteLine(sql.ToString());
#endif
            object retValue = SqlDBHelper.GetSingle(sql.ToString());
            function.AID = Convert.ToInt32(retValue);
            return function.AID;
        }

        public bool Update(CallContext context, FunctionInfo function)
        {
            SqlUpdate update = new SqlUpdate(FunctionTable);
            update.AddBusinessObject(function);
            update.AddCondition(new SqlCondition("AID", function.AID));
#if SHOW_SQL
            System.Diagnostics.Debug.WriteLine(update.ToString());
#endif
            return 0 < SqlDBHelper.ExecuteSql(update.ToString());
        }

        public bool Delete(CallContext context, FunctionInfo function)
        {
            SqlDelete delete = new SqlDelete(FunctionTable);
            delete.AddCondition(new SqlCondition("AID", function.AID));
#if SHOW_SQL
            System.Diagnostics.Debug.WriteLine(delete.ToString());
#endif
            return 0 < SqlDBHelper.ExecuteSql(delete.ToString());
        }


        public int BatchDelete(CallContext context, IList<int> idList)
        {
            SqlBatch batch = new SqlBatch();
            foreach (int id in idList)
            {
                SqlDelete delete = new SqlDelete(FunctionTable);
                delete.AddCondition(new SqlCondition("AID", id));
                batch.Add(delete);
            }

#if SHOW_SQL
            System.Diagnostics.Debug.WriteLine(batch.ToString());
#endif
            return SqlDBHelper.ExecuteSql(batch.ToString());
        }

        public int BatchSetModule(CallContext context, int moduleID, IList<int> functionIDList)
        {
            SqlBatch batch = new SqlBatch();
            foreach (int id in functionIDList)
            {
                SqlBuilder sql = new SqlBuilder();
                sql.Add("UPDATE " + FunctionTable + " SET ", new SqlExpression("ModuleID", id));
                batch.Add(sql);
            }

#if SHOW_SQL
            System.Diagnostics.Debug.WriteLine(batch.ToString());
#endif
            return SqlDBHelper.ExecuteSql(batch.ToString());
        }
    }
}
